import Taro from "@tarojs/taro";

type cartList = {
  id: string;
  num: number;
};

export default (id: string, num: number) => {
  let temp = Taro.getStorageSync("cartList");
  if (!temp) {
    temp = "[]";
  }
  const list = JSON.parse(temp);

  console.log(list);
  let flag = true;
  list.map(item => {
    if (item.id == id) {
      item.num += num;
      flag = false;
    }
  });

  if (flag) {
    list.push({ id, num });
  }

  Taro.setStorageSync("cartList", JSON.stringify(list));
};
